<!DOCTYPE  html>

<head>

<title>js—闭包 专项练习</title>
<meta charset="utf-8">
<style>
 .practice-content {
  position: relative;
  left: 0;
  top: 0;
  bottom: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  overflow-x: hidden;
}

.practice-title {
  text-align: center;
  background: #ffecf5;
  box-shadow: #fff7fb;
  height: 40px;
  font-size: 20px;
  font-weight: bold;
  padding-top: 10px;
}

.practice-information {
  margin-top: 20px;
  text-align: center;
  background: #ffecf5;
  box-shadow: #fff7fb;
  height: 40px;
  font-size: 20px;
  font-weight: bold;
  padding-top: 10px;
}

.information {
  margin-top: 20px;
  text-align: center;
  background: #ffecf5;
  box-shadow: #fff7fb;
  height: 400px;
  font-size: 20px;
  font-weight: bold;
  padding-top: 10px;
}
</style>

</head>

 <body>

<div class="practice-content">
	<div class="information">
		<p>js—闭包 专项练习</p>
		<p>
			<a target="_blank" href='https://www.cnblogs.com/jelina/p/11146345.html'>
			https://www.cnblogs.com/jelina/p/11146345.html</a>
		</p>
		 
	</div>
	 
</div>
 
<script>

 
console.log(window, '===window===');
 
function f1(){
	n = 4;  //n是局部变量;因为后面有个n的定义；相当于先定义var n;后赋值n = 999;
　　nAdd = 2;
    nAdd2 = function(){
		n += 1;
		console.log(n,nAdd,'=1000？,2===nAdd2====');
	}
	var n = 999;
　　function f2(){
　　　　console.log(n,nAdd,'=999,2===f2====');
　　}

　　return f2;
}
//console.log('===f1函数赋给变量前，nAdd2,nAdd,undefined==');

//var result = f1(); //将函数f1赋值给result变量result 指的是内部返回的f2
//result(); // 999
 f1(); //将函数f1赋值给window；f1()指的是内部return 的 f2
console.log(nAdd2,nAdd,'==f1函数赋给变量后，nAdd2,nAdd有致了==');

nAdd2();  //先将f1函数赋值，才能执行nAdd2函数，否则报nAdd2错误；

f1()(); //f1()() = result();//执行函数f1



// nAdd2();


console.log(window,nAdd,'===window2===');
//nAdd();

//result(); // 1000

function makeAdder(x) {
  return function(y) {
    return x + y;
  };
}

var add5 = makeAdder(5);
 
console.log(add5,'===add5====')
var add10 = makeAdder(10);

console.log(add5(2));  // 7
console.log(add10(2)); // 12

<!-- var name = "The Window";    -->
<!-- var object = { -->
	
<!-- 　　name : "My Object",    -->
<!-- 　　getNameFunc : function(){    -->
<!-- 　　　　return function(){    -->
<!-- 　　　　　　return this.name;    -->
<!-- 　　　　}; -->
<!-- 　　} -->
<!-- }; -->
<!-- alert(object.getNameFunc()());	 -->



<!-- window.addEventListener('load',function(){ -->
	<!-- for( var i=1; i<4; i++ ){ -->
	
		<!-- var btn = document.getElementById('btn' + i); -->
		
		<!-- //由于循环截止之后，最终的i的值为4，所以不管哪个按钮点击，打印的都是4 -->
		<!-- btn.addEventListener('click',function(e){ -->
			<!-- console.log(e.target); -->
			<!-- alert(i); -->
		<!-- }); -->
		
		<!-- /** -->
		<!-- btn.addEventListener('click',function(i){ -->
			<!-- alert(i); -->
		<!-- }(i)); -->
		<!-- **/ -->
		
		<!-- //btn.addEventListener('click',show(i)); -->
	<!-- } -->
<!-- }) -->

</script>

  </body>

</html>